package client;


import defaultProperties.window;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * addMembersToGroup.java
 *
 * Created on Oct 3, 2013, 8:09:56 AM
 */
/**
 *
 * @author GON
 */
public class addMembersToGroup extends window {

    ArrayList everyone, groupMembers;
    chat parent;
    int posX = 0, posY = 0;

    /** Creates new form addMembersToGroup */
    public addMembersToGroup(ArrayList everyOne, ArrayList gMembers, chat p) {
        // add default window propertes
        super.addWindowProperties(this);

        parent = p;
        everyone = everyOne;
        groupMembers = gMembers;
        initComponents();
        System.out.println("Everyone: " + everyone);
        if (everyone != null) {
            final String[] ol = new String[everyone.size()];
            for (int i = 0; i < everyone.size(); i++) {
                ol[i] = everyone.get(i).toString();
            }

            onlineUsers.setModel(new javax.swing.AbstractListModel() {

                String[] strings = ol;

                public int getSize() {
                    return strings.length;
                }

                public Object getElementAt(int i) {
                    return strings[i];
                }
            });

            if (groupMembers != null) {
                final String[] gm = new String[groupMembers.size()];
                for (int i = 0; i < groupMembers.size(); i++) {
                    gm[i] = groupMembers.get(i).toString();
                }

                groupMembersList.setModel(new javax.swing.AbstractListModel() {

                    String[] strings = gm;

                    public int getSize() {
                        return strings.length;
                    }

                    public Object getElementAt(int i) {
                        return strings[i];
                    }
                });
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        windowName = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        onlineUsers = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        groupMembersList = new javax.swing.JList();
        toGroupMembersButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setLocationByPlatform(true);
        setResizable(false);
        setUndecorated(true);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(229, 229, 229), 3));

        windowName.setFont(new java.awt.Font("Segoe UI Light", 0, 24));
        windowName.setText("Add Members");

        cancelButton.setFont(new java.awt.Font("Segoe UI Light", 0, 12));
        cancelButton.setText("Cancel");
        cancelButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        saveButton.setFont(new java.awt.Font("Segoe UI Light", 0, 12));
        saveButton.setText("Save");
        saveButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        jScrollPane2.setBackground(new java.awt.Color(204, 204, 204));
        jScrollPane2.setBorder(null);

        onlineUsers.setBackground(new java.awt.Color(231, 231, 231));
        onlineUsers.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204), 3));
        onlineUsers.setFont(new java.awt.Font("Segoe UI Light", 0, 12));
        jScrollPane2.setViewportView(onlineUsers);

        jScrollPane3.setBackground(new java.awt.Color(204, 204, 204));
        jScrollPane3.setBorder(null);

        groupMembersList.setBackground(new java.awt.Color(231, 231, 231));
        groupMembersList.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204), 3));
        groupMembersList.setFont(new java.awt.Font("Segoe UI Light", 0, 12));
        jScrollPane3.setViewportView(groupMembersList);

        toGroupMembersButton.setFont(new java.awt.Font("Segoe UI Light", 0, 12));
        toGroupMembersButton.setText(">>");
        toGroupMembersButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        toGroupMembersButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toGroupMembersButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Segoe UI Light", 0, 12));
        jLabel1.setText("Online Users:");

        jLabel2.setFont(new java.awt.Font("Segoe UI Light", 0, 12));
        jLabel2.setText("Group Members:");

        jLabel3.setFont(new java.awt.Font("Segoe UI Light", 0, 12));
        jLabel3.setForeground(new java.awt.Color(216, 216, 216));
        jLabel3.setText("___________________________________________________________________________");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 14, 14)
                        .addComponent(toGroupMembersButton, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(windowName))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(windowName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(2, 2, 2)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane3)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap(110, Short.MAX_VALUE)
                        .addComponent(toGroupMembersButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(103, 103, 103)))
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_cancelButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        parent.setGroupMembers(groupMembers);
        this.setVisible(false);
}//GEN-LAST:event_saveButtonActionPerformed

    private void toGroupMembersButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toGroupMembersButtonActionPerformed
        String user = onlineUsers.getSelectedValue().toString();
        if (everyone != null) {
            everyone.remove(user);
            final String[] ol = new String[everyone.size()];
            for (int i = 0; i < everyone.size(); i++) {
                ol[i] = everyone.get(i).toString();
            }

            onlineUsers.setModel(new javax.swing.AbstractListModel() {

                String[] strings = ol;

                public int getSize() {
                    return strings.length;
                }

                public Object getElementAt(int i) {
                    return strings[i];
                }
            });

            if (groupMembers == null) {
                groupMembers = new ArrayList();
            }
            System.out.println(user);
            groupMembers.add(user);
            final String[] gm = new String[groupMembers.size()];
            for (int i = 0; i < groupMembers.size(); i++) {
                gm[i] = groupMembers.get(i).toString();
            }

            groupMembersList.setModel(new javax.swing.AbstractListModel() {

                String[] strings = gm;

                public int getSize() {
                    return strings.length;
                }

                public Object getElementAt(int i) {
                    return strings[i];
                }
            });
        }
    }//GEN-LAST:event_toGroupMembersButtonActionPerformed
    /**
     * @param args the command line arguments
     */
    /*
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new addMembersToGroup().setVisible(true);
    }
    });
    }
     * 
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JList groupMembersList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList onlineUsers;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton toGroupMembersButton;
    private javax.swing.JLabel windowName;
    // End of variables declaration//GEN-END:variables

    void setUsersOnline(ArrayList everyone) {
        if (everyone != null) {
            final String[] ol = new String[everyone.size()];
            for (int i = 0; i < everyone.size(); i++) {
                if(!groupMembers.contains(everyone.get(i))){
                    ol[i] = everyone.get(i).toString();
                }
            }

            onlineUsers.setModel(new javax.swing.AbstractListModel() {

                String[] strings = ol;

                public int getSize() {
                    return strings.length;
                }

                public Object getElementAt(int i) {
                    return strings[i];
                }
            });
        }
    }
}
